<template>
  <div class="userManage">
    <CustomForm :formInline="form" @onSubmit="initial" @resetTable="resetTable">
      <el-form-item label="用户姓名">
        <el-input v-model="form.name" placeholder="输入用户姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.tel" placeholder="输入手机号"></el-input>
      </el-form-item>
    </CustomForm>
    <div class="tool">
      <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
    </div>
    <el-table border :data="tableData" style="width: 100%" height="620">
      <el-table-column prop="user_id" label="用户ID" align="center"></el-table-column>
      <el-table-column prop="name" label="用户名" align="center"></el-table-column>
      <el-table-column prop="tel" label="手机号" align="center"></el-table-column>
      <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column>
      <el-table-column label="操作"  align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editPhone(scope.row)">修改手机号</el-button>
          <el-button type="primary" size="mini" @click="setAuth(scope.row)">权限</el-button>
          <el-button type="danger" size="mini" @click="deleteUser(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="$pageSizeList"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
      <AddUser @close="()=> dialogVisible = false" v-if="dialogVisible" @sureAddUser="sureAddUser"  />
      <!-- <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>-->
    </el-dialog>

    <el-dialog class="dataDialog" title="设置权限" :visible.sync="dialogVisible2" width="50%" top="5vh" :close-on-click-modal="false">
      <SetAuthModal :userId="userId" @close="()=> dialogVisible2 = false" v-if="dialogVisible2"  />

    </el-dialog>

    <el-dialog title="修改手机号" :visible.sync="editVisable" width="25%" :close-on-click-modal="false">
      <editPhone :row="row" @close="editVisable = false,initial()" v-if="editVisable"  />

    </el-dialog>


  </div>
</template>

<script>
import { getUserList, deleteUser } from "@/api/set";
import AddUser from "./components/addUser";
import SetAuthModal from "./components/setAuthModal";
import editPhone from "./components/editPhone";
export default {
  name: "userManage",
  components: {
    AddUser,
    SetAuthModal,
    editPhone
  },
  data() {
    return {
      userId:'',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      form: {
        name: "",
        tel:""
      },
      row:{},
      editVisable:false,
      dialogVisible: false,
      dialogVisible2:false,
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
      let result = await getUserList({
        page: this.currentPage,
        pageSize: this.pageSize,
        ...this.form
      });
      // console.log("result", result);
      if (result.code == 200) {
        this.tableData = result.data.list;
        this.total = result.data.total;
      }
    },
    editPhone(row){
      this.row = row;
      this.editVisable = true;
    },
    sureAddUser(){
      this.dialogVisible = false;
      this.initial();
    },
    setAuth(row){
      this.userId = row.user_id + '';
      this.dialogVisible2 = true;
    },
    resetTable() {
      this.form = {
        name: "",
        tel:""
      };
      this.currentPage = 1;
      this.initial();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.currentPage = 1;
      this.initial();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.initial();
    },
    deleteUser(row) {
      console.log("row", row);
      this.$confirm("确定要删除该用户吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let result = await deleteUser({
            user_id: row.user_id
          });
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.currentPage = 1;
            this.initial();
          }
        })
        .catch(() => {
          //   this.$message({
          //     type: "info",
          //     message: "已取消删除"
          //   });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.userManage {
  background: #fff;
  padding-top: 10px;
  .tool {
    display: flex;
    justify-content: flex-end;
    padding: 10px;
  }
  .block {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
  .dataDialog{
    ::v-deep .el-dialog{
      height: 85vh;
      overflow: auto;
    }
  }
}
</style>
